<?php
class Core_Model_Skill 
	extends Core_Model_Abstract 
	implements Core_Model_Syneregy_Interface,
		Core_Model_Object_Interface
{
	const OBJ_TYPE		= 'Skill';
	
	const FETCH_ID		= 'Id';
	const FETCH_NAME	= 'Name';
	const FETCH_SYSTEM	= 'System';
	
	const B_UNTRAINED	= 1;
	const B_ARMOR		= 2;
	const B_RETRY		= 4;
	const B_TAKE10		= 8;
	const B_TAKE20		= 16;
	
	protected $id;
	
	protected $name;
	
	protected $check;
	
	protected $ability;
	
	protected $system;
	
	protected $parent;
	
	protected $time			= 0;
	
	protected $syneregy		= false;
	
	protected $restrictions	= 0;
	
	/** Syneregy */
	public function getSynObjectType()
	{
		return self::OBJ_TYPE;
	}
	
	public function getSynObjectId()
	{
		return $this->_id;
	}
	
	public function setSyneregy($syneregy)
	{
		$this->syneregy		= $syneregy;	
	}
	
	public function getSyneregy()
	{
		return $this->syneregy;
	}
		
	public function setParent($skill)
	{
		if (null == $skill)
		{
			return true;
		}
		
		if (is_array($skill))
		{
			$skill	= new Core_Model_Skill($skill);
		}
		
		if ((int) $skill > 0)
		{
			$skill	= new Core_Model_Skill(array('id' => $skill));
			$skill->loadSkill();
		}
		
		if ($skill instanceof Core_Model_Skill)
		{
			$this->parent		= $skill;			
		}
	}
	
	public function getObjectType()
	{
		return self::OBJ_TYPE;
	}
	
	public function getObjectDescp()
	{
		return self::OBJ_TYPE;
	}
	
	public function setObjectId($id)
	{
		return $this->setId($id);
	}
	
	public function loadObject()
	{
		return $this->loadSkill();
	}	

	/**
	 * 00001 - Untrained
	 * 00010 - Armor Penalty
	 * 00100 - Retry
	 * 01000 - Take 10
	 * 10000 - Take 20
	 */
	public function setRestrictions($bits)
	{
		$this->restrictions	= (int) $bits;
	}
	
	public function setAbility($ability)
	{
		if ($ability instanceof Core_Model_Ability)
		{
			$ability	= $ability->getType();
		}

		$ability	= Core_Model_Ability::validAbility($ability);
		if ($ability === false)
		{
			throw new Core_Model_Skill_Exception('Invalid Ability');
		}		
		
		$this->ability	= $ability;
	}
	
	public function setSystem($system)
	{
		if ($system instanceof Core_Model_Skill)
		{
			$system	= $system->getCommon();
		}
		
		if (!is_array($system))
		{
			$this->system[]	= $system;
		}
		else 
		{
			$this->system	= $system;
		}
	}
	
	public function setArmor($armor)
	{
		//turn on 
		if ($armor && !$this->getArmor())
		{
			$this->restrictions	+= self::B_ARMOR;
		}
		
		//turn off
		if (!$armor && $this->getArmor())
		{
			$this->restrictions	-= self::B_ARMOR;
		}
			
	}
	
	public function setReTry($retry)
	{
		//turn on 
		if ($retry && !$this->getRetry())
		{
			$this->restrictions	+= self::B_RETRY;
		}
		
		//turn off
		if (!$retry && $this->getRetry())
		{
			$this->restrictions	-= self::B_RETRY;
		}
	}
	
	public function setUntrained($untrained)
	{
		//turn on 
		if ($untrained && !$this->getUntrained())
		{
			$this->restrictions	+= self::B_UNTRAINED;
		}
		
		//turn off
		if (!$untrained && $this->getUntrained())
		{
			$this->restrictions	-= self::B_UNTRAINED;
		}
	}
	
	public function setTake10($take10)
	{
		//turn on 
		if ($take10 && !$this->getTake10())
		{
			$this->restrictions	+= self::B_TAKE10;
		}
		
		//turn off
		if (!$take10 && $this->getTake10())
		{
			$this->restrictions	-= self::B_TAKE10;
		}
	}
		
	public function setTake20($take20)
	{
		//turn on 
		if ($take20 && !$this->getTake20())
		{
			$this->restrictions	+= self::B_TAKE20;
		}
		
		//turn off
		if (!$take20 && $this->getTake20())
		{
			$this->restrictions	-= self::B_TAKE20;
		}
	}
	
	public function getArmor()
	{
		return ($this->restrictions & self::B_ARMOR) > 0;
	}

	public function getUntrained()
	{
		return ($this->restrictions & self::B_UNTRAINED) > 0;
	}

	public function getRetry()
	{
		return ($this->restrictions & self::B_RETRY) > 0;
	}

	public function getTake10()
	{
		return ($this->restrictions & self::B_TAKE10) > 0;
	}

	public function getTake20()
	{
		return ($this->restrictions & self::B_TAKE20) > 0;
	}
	
	public function getParent()
	{
		if (null == $this->parent)
		{
			return false;
		}
		
		if (!$this->parent instanceof Core_Model_Skill)
		{
			$this->parent	= new Core_Model_Skill(array('id' => $this->parent));
			$this->parent->load();
		}
		
		return $this->parent;
	}
	
	public function loadSkill($fetchOpt = null)
	{
		$mapper	= new Core_Model_Skill_Mapper();
		
		switch($fetchOpt)
		{
			case self::FETCH_ID:
			default:
				$mapper->fetchId($this);			
		}
	}
}